| 1 |  |  | import { Inject } from '@nestjs/common'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | import { CommandHandler } from '@nestjs/cqrs'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | import { CreateProjectCommand } from './CreateProjectCommand'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | import { IProjectRepository } from 'src/Domain/Project/Repository/IProjectRepository'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | import { Project } from 'src/Domain/Project/Project.entity'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | import { CustomerNotFoundException } from 'src/Domain/Customer/Exception/CustomerNotFoundException'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | import { IsProjectAlreadyExist } from 'src/Domain/Project/Specification/IsProjectAlreadyExist'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | import { ProjectAlreadyExistException } from 'src/Domain/Project/Exception/ProjectAlreadyExistException'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | import { ICustomerRepository } from 'src/Domain/Customer/Repository/ICustomerRepository'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | @CommandHandler(CreateProjectCommand) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | export class CreateProjectCommandHandler { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |   constructor( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     @Inject('IProjectRepository') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     private readonly projectRepository: IProjectRepository, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     @Inject('ICustomerRepository') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     private readonly customerRepository: ICustomerRepository, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     private readonly isProjectAlreadyExist: IsProjectAlreadyExist | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 19 |  |  |   ) {} | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |   public async execute(command: CreateProjectCommand): Promise<string> { | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     const { name, dayDuration, customerId, invoiceUnit } = command; | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     const customer = await this.customerRepository.findOneById(customerId); | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     if (!customer) { | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |       throw new CustomerNotFoundException(); | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     if (true === (await this.isProjectAlreadyExist.isSatisfiedBy(name))) { | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |       throw new ProjectAlreadyExistException(); | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     const project = await this.projectRepository.save( | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |       new Project(name, dayDuration, invoiceUnit, customer) | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     ); | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     return project.getId(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |   } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 39 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 40 |  |  |  |